<template>
  <a-row :wrap="false">
    <!-- // 标题 -->
    <a-col flex="200px">
      <RouterLink to="/">
        <div class="title-bar">
          <img class="logo" src="../assets/logo.png" alt="logo" />
          <div class="title">智能云图库</div>
        </div>
      </RouterLink>
    </a-col>
    <!--  // 菜单 -->
    <a-col flex="auto">
      <div id="globalHeader">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"


        />
      </div>
    </a-col>
    <!-- // 登录 -->
    <a-col flex="200px">
      <div class="user-login-status">
        <div v-if="loginUserStore.loginUser.id">

          <a-dropdown>
            <ASpace>
              <a-avatar :src="loginUserStore.loginUser.userAvatar" />

              {{ loginUserStore.loginUser.userName ?? '无名' }}
<!--              管理员专属图标-->
              <a-avatar v-if="loginUserStore.loginUser.userRole === 'admin'" :src="mb"

                        :size="15"
              />



            </ASpace>
            <template #overlay>
              <a-menu>
                <a-menu-item>
                  <router-link to="/my_space">
                    <UserOutlined />
                    我的空间
                  </router-link>
                </a-menu-item>
                <a-menu-item>
                  <router-link to="/user/edit">
                    <SolutionOutlined />
                    个人信息
                  </router-link>
                </a-menu-item>

                <a-menu-item @click="doLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>


              </a-menu>
            </template>
          </a-dropdown>
        </div>
        <div v-else>
          <a-button type="primary" href="/user/login">登录</a-button>
        </div>
      </div>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import {
  UnorderedListOutlined,
  GithubOutlined,
  HomeOutlined,
  LogoutOutlined,
  UserOutlined,
  SolutionOutlined, FireOutlined
} from '@ant-design/icons-vue'
import { type MenuProps, message } from 'ant-design-vue'

const current = ref<string[]>(['home'])
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/commentManage',
    label: '评论管理',
    title: '评论管理',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: '/add_space',
    label: '创建空间',
    title: '创建空间',
  },





  {
    key: '/recreation',
    label: '娱乐',
    title: '娱乐',
    icon: () => h( UnorderedListOutlined),

    children: [
      {
        key: '/recreation/weather',
        label: '天气',
        title: '天气',
      },

      {
        key: '/recreation/zodiac-fortune',
        label: '星座运势',
        title: '星座运势',
      },
      {
        key: '/recreation/news-display',
        label: '新闻热榜',
        title: '新闻热榜',
      },
      {
        key: '/recreation/music-player',
        label: '音乐播放器',
        title: '音乐播放器',
      },
      {
        key: '/recreation/ai-guess',
        label: 'AI海龟汤',
        title: 'AI海龟汤',
      },
      {
        key: '/generator',
        label: '多功能图片生成器',
        icon: () => h( FireOutlined),


      },
    ],
  },
/*  {
    key: '/weather',
    label: '天气',
    title: '天气',

  },
  {
    key: '/huangli',
    label: '黄历',
    title: '黄历',
  },*/
  {
    key: 'others',
    label: h(
      'a',
      {
        href: 'https://gitee.com/zheng-fengnan/online-picture-vue',
        target: '_blank',

      },
      'Git源码',
    ),
    title: 'Git源码',
    icon: () => h( GithubOutlined),
  },
]
// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu.key.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

const mb = "https://grape-1339488794.cos.ap-guangzhou.myqcloud.com/admin.png"

// 路由跳转事件



// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'

const router = useRouter()
// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}
// 监听路由变化，更新当前选中菜单
router.afterEach((to, from, next) => {
  current.value = [to.path]
})
const loginUserStore = useLoginUserStore()
// loginUserStore.fetchLoginUser()
// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  console.log(res)
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
